package com.douing.ad.dsp.bidding.utils;

import java.util.List;
import java.util.Map;
import java.util.Random;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * Redis工具类
 * 
 * @author hjf
 *
 */
public final class RedisUtils {
    public static final Logger log = LoggerFactory.getLogger(RedisUtils.class);

    private static JedisPool masterJedisPool;

    private static List<JedisPool> slaveJedisPool;
    
    public void setMasterJedisPool(JedisPool masterJedisPool) {
        this.masterJedisPool = masterJedisPool;
      }

      public void setSlaveJedisPool(List<JedisPool> slaveJedisPool) {
        this.slaveJedisPool = slaveJedisPool;
      }
    private RedisUtils() {}
   
    public static String get(final String key) {       
        Random random = new Random();
        int index = 0;//random.nextInt(slaveJedisPool.size()+1);
        String result = null;
        if (index == 0) {
            long time1 = System.currentTimeMillis();
            result = get0(key);
            long time2 = System.currentTimeMillis();
            log.info("index ====//===> get master total time : " + (time2 - time1));
            return result;
        } else {
            long time1 = System.currentTimeMillis();
            result = get1(key);
            long time2 = System.currentTimeMillis();
            log.info("index ====//===> get slave total time : " + (time2 - time1));
            return result;
        }
    }

    public static String get0(final String key) {
        Jedis jedis = null;
        String res = null;
        try {
            jedis = masterJedisPool.getResource();
            res = jedis.get(key);
        } catch (Exception ex) {
            log.error("com.douing.ad.dsp.business.common.redis.impl.JedisPoolManager.get错误", ex);
        } finally {
            if (null != jedis) {
                jedis.close();
            }
        }
        return res;
    }

    public static String get1(final String key) {
        Jedis jedis = null;
        String res = null;
        try {
            Random random = new Random();
            int index = random.nextInt(slaveJedisPool.size());
            jedis = slaveJedisPool.get(index).getResource();
            res = jedis.get(key);
        } catch (Exception ex) {
            log.error("com.douing.ad.dsp.business.common.redis.impl.JedisPoolManager.get错误", ex);
        } finally {
            if (null != jedis) {
                jedis.close();
            }
        }
        return res;
    }

    public static String set(final String key, final String value) {
        return masterJedisPool.getResource().set(key, value);
    }

    public static List<String> hmget(final String key, final String field) {
        return masterJedisPool.getResource().hmget(key, field);
    }

    public static List<String> hvals(final String key) {
        Random random = new Random();
        int index = 0;//random.nextInt(2);
        if (index == 0) {
            return hvals0(key);
        } else {
            return hvals1(key);
        }
    }

    public static List<String> hvals1(final String key) {
        Jedis jedis = null;
        List<String> res = null;
        try {
            Random random = new Random();
            int index = random.nextInt(slaveJedisPool.size());
            jedis = slaveJedisPool.get(index).getResource();
            res = jedis.hvals(key);
        } catch (Exception ex) {
            log.error("com.douing.ad.dsp.business.common.redis.impl.JedisPoolManager.get错误", ex);
        } finally {
            if (null != jedis) {
                jedis.close();
            }
        }
        return res;
    }

    public static List<String> hvals0(final String key) {
        Jedis jedis = null;
        List<String> res = null;
        try {
            jedis = masterJedisPool.getResource();
            res = jedis.hvals(key);
        } catch (Exception ex) {
            log.error("com.douing.ad.dsp.business.common.redis.impl.JedisPoolManager.get错误", ex);
        } finally {
            if (null != jedis) {
                jedis.close();
            }
        }
        return res;
    }

    public static String hmset(final String key, Map<String, String> hash) {
        return masterJedisPool.getResource().hmset(key, hash);
    }

}
